AddCSLuaFile( "cl_init.lua" )
AddCSLuaFile( "shared.lua" )
 
include('shared.lua')

local Kicked = false
local GotBall = false
local vel;
local kicktime = 0.7

function ENT:Initialize()

		self:SetModel( "models/props_phx/misc/soccerball.mdl" ) 
		self:PhysicsInit( SOLID_VPHYSICS )
		self:SetMoveType( MOVETYPE_VPHYSICS )
		self:SetSolid( SOLID_VPHYSICS )
		
		self:DrawShadow( false )
	 
		local phys = self:GetPhysicsObject()
		if (phys:IsValid()) then
		phys:Wake()
		end
		
end

function ENT:SpawnFunction( ply, tr )

	if ( !tr.Hit ) then return end
		
	local SpawnPos = tr.HitPos + tr.HitNormal * 16
	
	local ent = ents.Create( ClassName )
	ent:SetPos( SpawnPos )
	ent:Spawn()
	ent:Activate()
	ent:SetColor(0,0,255,150)

	return ent
		
end
function ENT:Touch( ply )
	if ValidEntity(ply) && ply:IsPlayer() then
		if ply:Alive() and GotBall == false then
			WorldSound( "soccer/gotball.wav", self.Entity:GetPos(), 160, 130 )
		end
		local phys = self:GetPhysicsObject()
			vel = self:GetPos() - ply:GetPos()
		if !ply:KeyDown(IN_SPEED) then
			vel = vel + ply:GetForward() * 200
		elseif ply:KeyDown(IN_SPEED) then
			vel = vel + ply:GetForward() * 400
		end
		phys:SetVelocity(vel)
		end
end
	
function ENT:Use( Activator, Caller )
	if !Caller:Alive() then return false end
	Kicked = true
	local phys = self:GetPhysicsObject()
	phys:SetVelocity(Caller:GetForward() * 600)
	Caller:EmitSound("soccer/kicked.wav",500,100)
    if Caller.StopKick and Caller.StopKick < CurTime() then
        Caller.Kicking = true
        Caller.KickTime = CurTime()
        Caller.StopKick = Caller.KickTime + kicktime
        umsg.Start("Kicking",Caller)
        umsg.Bool(true)
        umsg.End()
		timer.Simple(0.15, function()
            KickHit(Caller)
		end, Caller)
    end
end

function ENT:Think()
end

function ENT:OnRemove( )
	return false
end